﻿using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LdtDataEngin.ModuleDetail.BaseModule.Optimization
{
    public class SginOptimizationData : SgingOPtimizationBase
    {
        public SginOptimizationData(DataContext context)
        {
            dataContext = context;
        }

        /// <summary>
        /// 执行优选
        /// </summary>
        /// <param name="dataModelAsis">分析数据源</param>
        /// <param name="dataModelOP">优选数据源</param>
        /// <param name="opsum">优选数据集</param>
        /// <param name="assum">分析数据集</param>
        /// <param name="xmllableConfig">配置</param>
        /// <param name="soursnameopid">数据源id</param>
        /// <param name="soursnameasisid">数据源id</param>
        public void Optimization(DataModel dataModelAsis, DataModel dataModelOP, int opsum, int assum, Xmllable xmllableConfig, string soursnameopid, string soursnameasisid)
        {
            SgingBase(dataModelAsis, dataModelOP, opsum, assum, xmllableConfig, soursnameopid, soursnameasisid);
        }

        /// <summary>
        /// 执行优选操作
        /// </summary>
        /// <param name="dataModelAsisList">分析完整结果</param>
        /// <param name="dataModelsOPList">优选完整结果</param>
        /// <param name="analysisModel">分析实体</param>
        /// <param name="OptimizationResultsList">优选条件数据</param>
        public void OptimizationData(List<DataModel> dataModelAsisList, List<DataModel> dataModelsOPList, DataModel analysisModel, List<DataModel> OptimizationResultsList)
        {
            DataModel sginDataModel = new DataModel();
            foreach (Element element in xmlConfig.ElementList)
            {
                AttributeInfo attributeModel = analysisModel.AttributesList.FirstOrDefault(a => a.Name == element.Name);
                if (attributeModel != null)
                {
                    string elementValue = attributeModel.sourceList.FirstOrDefault(a => a.IsCheck == "1").SaveValue;
                    if (!string.IsNullOrEmpty(elementValue))
                    {

                        sginDataModel = OptimizationResultsList.FirstOrDefault(a => a.AttributesList.FirstOrDefault(b => b.Name == element.Name &&
                             b.sourceList.FirstOrDefault(s => s.IsCheck == "1" && s.SaveValue == elementValue) != null) != null);
                    }
                }
                if (sginDataModel != null)
                {
                    break;
                }
            }
            string opSoursNumber = Sourcedatadefine[0].SourcedataList.FirstOrDefault(a => a.Code == sginDataModel.sourceData.sourceList.FirstOrDefault(a => a.IsCheck == "1").SourceCode).id.ToString();
            string analysisNumber = Sourcedatadefine[0].SourcedataList.FirstOrDefault(a => a.Code == dataContext.DataLayerAns.SourceCode).id.ToString();
            int opNumber = ListByCount(xmlConfig.Priority.Split('>').ToList(), opSoursNumber);
            int analyNumber = ListByCount(xmlConfig.Priority.Split('>').ToList(), analysisNumber);
            bool determine = true;

            #region 节点优选数据
            //分析数据优先级高
            if (analyNumber < opNumber)
            {
                if (sginDataModel.sourceData.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower()) != null)
                {
                    foreach (Source sginsoure in sginDataModel.sourceData.sourceList)
                    {
                        if (sginsoure.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower())
                        {
                            sginsoure.IsCheck = "1";
                        }
                        else
                        {
                            sginsoure.IsCheck = "0";
                        }
                    }
                }
                else
                {
                    Source source = analysisModel.sourceData.sourceList[0];
                    foreach (Source sginsoure in sginDataModel.sourceData.sourceList)
                    {
                        sginsoure.IsCheck = "0";
                    }
                    sginDataModel.sourceData.sourceList.Add(source);
                }
            }
            //结果数据优先级高
            if (analyNumber > opNumber)
            {
                if (sginDataModel.sourceData.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower()) == null)
                {
                    Source source = analysisModel.sourceData.sourceList[0];
                    sginDataModel.sourceData.sourceList.Add(source);
                }
            }
            //双方优先级相等
            if (analyNumber == opNumber)
            {
                Source ansislySoure = analysisModel.sourceData.sourceList[0];
                Source opSoure = sginDataModel.sourceData.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower());
                string ischck = opSoure.IsCheck;
                ansislySoure.IsCheck = ischck;
                opSoure = ansislySoure;
            }
            #endregion

            #region 属性优选
            foreach (AttributeInfo asisylAttribute in analysisModel.AttributesList)
            {
                if (sginDataModel.AttributesList.FirstOrDefault(a => a.Name == asisylAttribute.Name && a.Name != "id") == null)
                {
                    AttributeInfo attributeTmp = new AttributeInfo();
                    attributeTmp.Name = asisylAttribute.Name;
                    attributeTmp.DictionariesCode = asisylAttribute.DictionariesCode;
                    attributeTmp.Priority = asisylAttribute.Priority;
                    attributeTmp.sourceList = asisylAttribute.sourceList;
                    sginDataModel.AttributesList.Add(attributeTmp);
                }
                else
                {
                    AttributeInfo attributeop = sginDataModel.AttributesList.FirstOrDefault(a => a.Name == asisylAttribute.Name && a.Name != "id");
                    if (attributeop.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode) == null)
                    {
                        Source attsoucremodel = asisylAttribute.sourceList[0];
                        attsoucremodel.IsCheck = "0";
                        attributeop.sourceList.Add(attsoucremodel);
                    }
                    if (xmlConfig.ElementList != null)
                    {
                        Element element = xmlConfig.ElementList.FirstOrDefault(a => a.Name == asisylAttribute.Name);
                        if (element != null && !string.IsNullOrEmpty(element.Priority))
                        {
                            if (element.ConditionList.Count > 0)
                            {

                                try
                                {
                                    determine = ConditionDetermine(element.ConditionList);
                                }
                                catch (System.Exception ef)
                                {
                                    dataContext.InteractiveShow.LogShow("当前数据项条件解析错误，配置信息：" + JsonConvert.SerializeObject(ef.ToString()));
                                    dataContext.InteractiveShow.LogShow("当前数据项条件解析错误，错误信息：" + ef.ToString());
                                }



                            }
                            opNumber = ListByCount(element.Priority.Split('>').ToList(), opSoursNumber);
                            analyNumber = ListByCount(element.Priority.Split('>').ToList(), analysisNumber);
                        }
                    }


                    if (!determine)
                    {
                        return;
                    }
                    if (analyNumber < opNumber)
                    {
                        foreach (Source attsource in attributeop.sourceList)
                        {

                            if (attsource.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower())
                            {
                                attsource.IsCheck = "1";
                            }
                            else
                            {
                                attsource.IsCheck = "0";
                            }
                        }
                    }
                    else if (analyNumber == opNumber)
                    {
                        try
                        {


                            LdtJudge.DataEngine.Model.XMLConfigModel.DataStores.Source sourceasis = asisylAttribute.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower());
                            LdtJudge.DataEngine.Model.XMLConfigModel.DataStores.Source sourceop = attributeop.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower());
                            attributeop.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower()).SaveValue = sourceasis.SaveValue;
                            sourceop = sourceasis;
                        }
                        catch (System.Exception)
                        {
                           
                        }
                    }
                }
                if (asisylAttribute.sourceList.FirstOrDefault(a=> a.SourceCode==null)!=null)
                {
                    asisylAttribute.sourceList.FirstOrDefault(a => a.SourceCode == null).SourceCode = dataContext.DataLayerAns.SourceCode.ToLower();
                }
            }
            #endregion
        }

    }
}